package com.fingard.dsp.bank.directbank.boc02.utils;


import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;

public class AES {
	public static final String CHAR_ENCODING = "UTF-8";
	public static final String AES_ALGORITHM = "AES/ECB/PKCS5Padding";



	/**
	 * 加密
	 *
	 * @param content
	 * 需要加密的内容
	 * @return
	 */
	public static String encrypt(byte[] content,byte[] key) {

		try {
			Cipher cipher = Cipher.getInstance(AES_ALGORITHM);// 创建密码器
			cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key,"AES"));// 密钥初始化
			byte[] result = cipher.doFinal(content);  // 加密
			return new String(Base64.encodeBase64(result));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}


	public static String encryptToBase64(String data, String key) {
		try {
			return encrypt(data.getBytes(CHAR_ENCODING), key.getBytes(CHAR_ENCODING));
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException("encrypt fail!", e);
		}

	}

}
